<?php
namespace Admin\Controller;
use Think\Controller;

class AdminSystemNavigationController extends BaseController {
    
    public function listNav($flag){
       $m = M('SysSystemnavigation');
       $result = $m->where("pid = 0")->order('orderIndex')->find();
       if($result>0){
       		$list = array();
       		//array_push($list, $result);
       		$childrens = $this->recursion($result);
       		//$list['parent'] = $childrens;
       		$list = $childrens;
       		//var_dump(json_encode($list));
       		$this->ajaxReturn($list);
       }
      
       //$this->success("登陆成功！");
    }


    public function listNavAdmin(){
      	 $this->listNav('listNavAdmin');
    }

	/*
	 * 递归遍历
	 * @param $data array
	 * @param $id int
	 * return array
	 * */
	function recursion($data) {
		 $temp = array();
		 $m = M('SysSystemnavigation');
		// $data['parent'] = $data;
	     $children = $m->where("pid = '{$data['id']}'")->order('orderIndex asc')->select();
		 foreach($children as $v) {
		 	   $v['children'] = $this->recursion($v);
		   	//    if(empty($v['children'])) {
			  	//   unset($v['children']);
			   // }
			   $v['attributes']['url'] = $v['url'];
		   	   array_push($temp, $v); 
		  }
          //$data['children'] = $temp;
	      return $temp;
	}

	/** 递归调用得到所有子节点 */
	// private SystemNavigationDTO listChildren(SystemNavigation systemNavigation) {
	// 	SystemNavigationDTO sysTreeDTO = new SystemNavigationDTO(systemNavigation);
	// 	List<SystemNavigation> children = systemNavigationDao.list(" from SystemNavigation s where s.pid=? order by orderIndex ", systemNavigation.getId());
	// 	List<SystemNavigationDTO> temp = new ArrayList<SystemNavigationDTO>();
	// 	for (SystemNavigation navigation : children) {
	// 		temp.add(listChildren(navigation));
	// 	}
	// 	sysTreeDTO.setChildren(temp);
	// 	return sysTreeDTO;
	// }

}